package com.erb.service.llb.impl;

import com.erb.constants.RabbitMqConstants;
import com.erb.entity.Invest;
import com.erb.entity.Redpacketrecord;
import com.erb.mapper.InvestMapper;
import com.erb.service.llb.IOtherService;
import com.erb.service.llb.InvestService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Transactional
@Service
public class InvestServiceImpl implements InvestService {


	@Autowired
	InvestMapper investMapper;
	@Autowired
	RabbitTemplate rabbitTemplate;
	@Autowired
	IOtherService otherService;

	@Override
	public int addInvest(Invest invest) {
		System.out.println("-----------" + invest);
		int i = investMapper.addInvest(invest);
		if (i > 0) {
			//更新项目已投金额
			otherService.updateProductHaveMoney(invest.getPid(), invest.getInvestMoney());
			//向队列发送一条消息
			rabbitTemplate.convertAndSend(RabbitMqConstants.ERB_EXCHANGE_NAME, RabbitMqConstants.ERB_KEY_NAME, invest, new CorrelationData(UUID.randomUUID().toString()));
			//更新用户余额
			//如果使用了红包
			Double consume = invest.getInvestMoney();
			if (null != invest.getRid() && 0 != invest.getRid()) {
				Redpacketrecord redpacketrecord = otherService.findRedById(invest.getRid());
				consume = consume - redpacketrecord.getAmount();
				//更新红包状态
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				otherService.updateRedState(redpacketrecord.getId(), 2, formatter.format(new Date()));
			}
			otherService.updateUserBalance(invest.getUid(), consume * -1);
		}
		return i;
	}

	@Override
	public List<Invest> findByStatus(int status) {
		return investMapper.findByStatus(status);
	}

	@Override
	public List<Invest> findByPid(int pid) {
		return investMapper.findByPid(pid);
	}

	@Override
	public int updateStatus(int id, int status) {
		return investMapper.updateStatus(id, status);
	}

	@Override
	public int batchUpdateStatus(int oldStatus, int newStatus) {
		return investMapper.batchUpdateStatus(oldStatus, newStatus);
	}

	@Override
	public int updateStatusByPid(int pid, int status) {
		return investMapper.updateStatusByPid(pid, status);
	}

	@Override
	public int updateLastProfitDate(int id, Timestamp lastProfitDate) {
		return investMapper.updateLastProfitDate(id, lastProfitDate);
	}

	@Override
	public Double countInvestMoney() {
		return investMapper.countInvestMoney();
	}

	@Override
	public int sumUsersByProType(int type) {
		return investMapper.sumUsersByProType(type);
	}

	@Override
	public int sumInvest() {
		return investMapper.sumInvest();
	}

	@Override
	public List<Map> countInvestByMoney(int pid) {
		return investMapper.countInvestByMoney(pid);
	}

	@Override
	public List<Map> countInvestBySex(int pid) {
		return investMapper.countInvestBySex(pid);
	}
}
